package com.yuanyu.server.impl;

import com.yuanyu.common.util.AssertUtil;
import com.yuanyu.common.util.CopyUtil;
import com.yuanyu.common.util.main.List1_Become_List2_Util;
import com.yuanyu.mapper.mysqlMapper.MainTable_Mapper;
import com.yuanyu.mapper.mysqlMapper.StaffMapper;
import com.yuanyu.model.dto.MainTableDTO;
import com.yuanyu.model.dto.ProcessDTO;
import com.yuanyu.model.entity.MainTable;
import com.yuanyu.model.vo.MainVO;
import com.yuanyu.model.vo.ProcessVO;
import com.yuanyu.server.IMainServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class MainServerImpl implements IMainServer {
    @Autowired
    private MainTable_Mapper mainMapper;
    @Autowired
    private StaffMapper staffMapper;

    @Override
    public Integer TotalNumberOfQueriedData() {
        return mainMapper.selAllTable();
    }

    @Override
    public Integer TotalFuzzyNumberOfQueriedData(MainVO mainVO) {
        return mainMapper.selFuzzy(mainVO);
    }

    @Override
    public List<MainTableDTO> mains(int page) {
        Integer number = (page - 1) * 10;
        List<MainTable> mainTables = mainMapper.selPageTable(number);
        return List1_Become_List2_Util.mainTableBecomeMainTables(mainTables);
    }

    @Override
    public List<MainTableDTO> fuzzyMains(MainVO mainVO, int page) {
        Integer number = (page - 1) * 10;
        System.out.println("mainVO.getRadio_state() = " + mainVO.getRadio_state());
        List<MainTable> mainTables = mainMapper.selFuzzyPageTable(mainVO, number);
        return List1_Become_List2_Util.mainTableBecomeMainTables(mainTables);
    }

    @Override
    public ProcessDTO process(String Case) {
        MainTable mainTable = mainMapper.selByCase(Case);
        ProcessDTO processDTO = new ProcessDTO();
        CopyUtil.copyClass(mainTable,processDTO);
        return processDTO;
    }

    @Override
    public String updateProcess(ProcessVO processVO) {
        MainTable mainTable = mainMapper.selByCase(processVO.getCase());
        AssertUtil.isFalse(mainTable.getState().equals("1"),"该工单已经完成");
        AssertUtil.isFalse(mainTable.getState().equals("2"),"该工单已经结单");
        int i = mainMapper.updateByCaseProcess(processVO);
        AssertUtil.isTrue(i == 1,"工序更改失败");
        return "工序更改成功";
    }

    @Override
    public String statements(String Case) {
        MainTable mainTable = mainMapper.selByCase(Case);
        AssertUtil.isFalse(mainTable.getState().equals("-1"),"该工单未设置工序");
        AssertUtil.isFalse(mainTable.getState().equals("0"),"该工单未完成全部工序");
        AssertUtil.isFalse(mainTable.getState().equals("2"),"该工单已结单");
//        AssertUtil.isNull(staffMapper.queryByCaseIdAll(Case),"该工单未完成全部工序");
        int i = mainMapper.updateState(Case, "2");
        AssertUtil.isTrue(i == 1,"结单未成功");
        return "结单成功";
    }

    @Override
    public String MandatoryStatements(String Case) {
        MainTable mainTable = mainMapper.selByCase(Case);
        AssertUtil.isFalse(mainTable.getState().equals("2"),"该工单已结单");
        int i = mainMapper.updateState(Case, "2");
        AssertUtil.isTrue(i == 1,"强制结单未成功");
        return "强制结单成功";
    }
}
